<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      @open="onOpen"
      @close="close"
    >
      <el-row :gutter="0">
        <el-form
          ref="elForm"
          :model="formData"
          :rules="rules"
          size="small"
          label-width="100px"
        >
          <el-col :span="24">
            <el-form-item label="选项名" prop="label">
              <el-input
                v-model="formData.label"
                placeholder="请输入选项名"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="选项值" prop="value">
              <el-input
                v-model="formData.value"
                placeholder="请输入选项值"
                clearable
              >
                <template #append>
                  <el-select v-model="dataType" :style="{ width: '100px' }">
                    <el-option
                      v-for="(item, index) in dataTypeOptions"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                      :disabled="item.disabled"
                    />
                  </el-select>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <template #footer>
        <div>
          <el-button type="primary" @click="handelConfirm"> 确定 </el-button>
          <el-button @click="close"> 取消 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, watch, onMounted, computed } from "vue";
import { isNumberStr } from "../utils/common";
import { getTreeNodeId, saveTreeNodeId } from "../utils/db";

const emit = defineEmits(["update:visible", "commit"]);

const id = ref(getTreeNodeId());
const formData = ref({
  label: undefined,
  value: undefined,
});
const rules = {
  label: [{ required: true, message: "请输入选项名", trigger: "blur" }],
  value: [{ required: true, message: "请输入选项值", trigger: "blur" }],
};
const dataType = computed(() =>
  isNumberStr(formData.value.value) ? "number" : "string"
);
const dataTypeOptions = [
  { label: "字符串", value: "string" },
  { label: "数字", value: "number" },
];

watch(id, (newVal) => {
  saveTreeNodeId(newVal);
});

watch(
  () => formData.value.value,
  (newVal) => {
    if (isNumberStr(newVal)) {
      dataType.value = "number";
    } else {
      dataType.value = "string";
    }
  }
);

const onOpen = () => {
  formData.value = {
    label: undefined,
    value: undefined,
  };
};

const close = () => {
  emit("update:visible", false);
};

const handelConfirm = () => {
  const elForm = ref(null);
  elForm.value.validate((valid) => {
    if (!valid) return;
    if (dataType.value === "number") {
      formData.value.value = parseFloat(formData.value.value);
    }
    formData.value.id = id.value;
    emit("commit", formData.value);
    close();
  });
};
</script>

<style lang="scss" scoped></style>
